<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

		function uploadFile($post,$file,$type,$uid){
			$error = "";
			$msg = "";
		
			$path = $post["path"];
			$name = $post["name"];
				
			$fileElementName = $name;  //传入文件元素
			$upload_dir=$path; //指定文件存储路径
			
			$upload_file=$file[$fileElementName]["name"];        //获取文件名
			$upload_tmp_file=$file[$fileElementName]["tmp_name"];      //获取临时文件名
			$upload_filetype=$file[$fileElementName]["type"];    //获取文件类型
			$upload_filesize=$file[$fileElementName]["size"];
			$upload_status=$file[$fileElementName]["error"];  //获取文件出错情况
			$uploadFlag=true;
			
			$image_width = "300";
			$max_file = "3"; //最大3M
			$allowed_image_types = array('image/pjpeg'=>"jpg",'image/jpeg'=>"jpg",'image/jpg'=>"jpg",'image/png'=>"png",'image/x-png'=>"png",'image/gif'=>"gif");
			$allowed_image_ext = array_unique($allowed_image_types); // do not change this
			$image_ext="";
			foreach ($allowed_image_ext as $mime_type => $ext) {
				$image_ext.= strtoupper($ext)." ";
			}
			
			$filename = basename($upload_file);
			$file_ext = strtolower(substr($filename, strrpos($filename, '.') + 1));

			
		//Only process if the file is a JPG, PNG or GIF and below the allowed limit
			if((!empty($file)) && ($upload_status == 0)) {
				foreach ($allowed_image_types as $mime_type => $ext) {
					if($file_ext==$ext && $upload_filetype==$mime_type){
						$error = "";
						break;
					}else{
						$error = "只支持【".$image_ext."】格式的图片，So...";
					}
				}

				if ($error=="" && $upload_filesize > ($max_file*1048576)) {
					$error.= "文件太大了，能不能小于 ".$max_file."M";
				}
			}
			else{
				$error= "请选择文件，谢谢~";
			}
		
			function createAvatarFile($userid){
				$lev1=floor($userid/1000000);
				$lev1 =str_pad($lev1,3,"0",STR_PAD_LEFT);
				$lev2=floor($userid/10000);
				$lev2 =str_pad($lev2,2,"0",STR_PAD_LEFT);
				$lev3=floor($userid/100);
				$lev3=str_pad($lev3,2,"0",STR_PAD_LEFT);
				$fileDir="/".$lev1."/".$lev2."/".$lev3;
				return($fileDir);
			}
		
			$upload_dir .= createAvatarFile($uid);

			if($error =="" && is_uploaded_file($upload_tmp_file)){  
				
				if(file_exists($upload_dir) && is_dir($upload_dir)){
				}else{
					mkdir($upload_dir,0777,true);
				}
				
				switch($upload_status)
				{
					case 0: $error= "";break;
					case 1:	$error= "上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值。";break;
					case 2:	$error= "上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。";break;
					case 3:	$error= "文件只有部分被上传。";break;
					case 4:	$error= "没有文件被上传。";break;
					case 6:	$error= "没有找到临时文件目录。";break;
					case 7:	$error= "文件写入失败。";break;
				}//分析文件出错情况并给出提示
				
				//生成文件路径
				$upload_path=$upload_dir."/".str_pad($uid%100,2,"0",STR_PAD_LEFT)."_avatar_".date("YmdHis",time()+8*3600).".".$ext;
				
				
				$width = getWidth($upload_tmp_file);
				$height = getHeight($upload_tmp_file);
				
				if($width>$height){
					$scale = $image_width/$height;
					$x=($width-$height)/2;
					$y=0;
					$side = $height;
				}
				else{
					$scale = $image_width/$width;
					$x=0;
					$y=($height-$width)/2;
					$side = $width;
				}
				
				//调整大小 为300*300
				resizeThumbnailImage($upload_path,$upload_tmp_file,$side,$side,$x,$y,$scale);
						
				if(is_uploaded_file($upload_tmp_file)){
			
					if(file_exists($upload_path)){	
						//echo "同名文件已经存在，请修改你要上传的文件名！";
						$uploadFlag=false;
						//检查是否有相同文件存在
					}else if(move_uploaded_file($upload_tmp_file,$upload_path)){
						//把文件从文件临时目录移动到上传目录
						$uploadFlag=true;
					}else{
						//echo"文件上传失败。";
						$uploadFlag=false;
					}
				}
				
				$upload_path=strtr($upload_path,"\\","/");			
			}
			else{  
		//echo "没有文件上传！";
				$upload_path=null;    
			}
		
			if($upload_path!=null){
			$msg=base_url().$upload_path;
			}else{
			}
	
			$result=Array($error,$msg);
			return($result);
		}

		
		
		function resizeImage($image,$width,$height,$scale) {
			list($imagewidth, $imageheight, $imageType) = getimagesize($image);
			$imageType = image_type_to_mime_type($imageType);
			$newImageWidth = ceil($width * $scale);
			$newImageHeight = ceil($height * $scale);
			$newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
			switch($imageType) {
				case "image/gif":
					$source=imagecreatefromgif($image);
					break;
				case "image/pjpeg":
				case "image/jpeg":
				case "image/jpg":
					$source=imagecreatefromjpeg($image);
					break;
				case "image/png":
				case "image/x-png":
					$source=imagecreatefrompng($image);
					break;
			}
			imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height);

			switch($imageType) {
				case "image/gif":
					imagegif($newImage,$image);
					break;
				case "image/pjpeg":
				case "image/jpeg":
				case "image/jpg":
					imagejpeg($newImage,$image,90);
					break;
				case "image/png":
				case "image/x-png":
					imagepng($newImage,$image);
					break;
			}

			chmod($image, 0777);
			return $image;
		}

		function resizeThumbnailImage($thumb_image_name, $image, $width, $height, $start_width, $start_height, $scale){
			list($imagewidth, $imageheight, $imageType) = getimagesize($image);
			$imageType = image_type_to_mime_type($imageType);

			$newImageWidth = ceil($width * $scale);
			$newImageHeight = ceil($height * $scale);
			$newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
			switch($imageType) {
				case "image/gif":
					$source=imagecreatefromgif($image);
					break;
				case "image/pjpeg":
				case "image/jpeg":
				case "image/jpg":
					$source=imagecreatefromjpeg($image);
					break;
				case "image/png":
				case "image/x-png":
					$source=imagecreatefrompng($image);
					break;
			}
			imagecopyresampled($newImage,$source,0,0,$start_width,$start_height,$newImageWidth,$newImageHeight,$width,$height);
			switch($imageType) {
				case "image/gif":
					imagegif($newImage,$thumb_image_name);
					break;
				case "image/pjpeg":
				case "image/jpeg":
				case "image/jpg":
					imagejpeg($newImage,$thumb_image_name,90);
					break;
				case "image/png":
				case "image/x-png":
					imagepng($newImage,$thumb_image_name);
					break;
			}
			chmod($thumb_image_name, 0777);
			return $thumb_image_name;
}

		function getHeight($image) {
			$size = getimagesize($image);
			$height = $size[1];
			return $height;
		}
		
		function getWidth($image) {
			$size = getimagesize($image);
			$width = $size[0];
			return $width;
		}

		function deldir($dir){
			if(!file_exists($dir))
			 return false;
			 
			if(unlink($dir)) {
				return true;
			} else {
				return false;
			}
		}
		
